package org.datascooter.test;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import org.datascooter.cache.CacheRegion;
import org.datascooter.impl.DataSnip;

class DelayedItem implements Delayed {

	private Object object;
	private final long time;
	public DataSnip dataSnip;

	public DelayedItem(CacheRegion region, Object value) {
		this.object = value;
		dataSnip = region.dataSnip;
		if (region.timeToLiveSeconds > 0) {
			time = System.nanoTime() + Math.round(region.timeToLiveSeconds * 1E9);
		} else {
			time = -1;
		}
	}

	@Override
	public int compareTo(Delayed o) {
		return Long.valueOf(getDelay(null) - o.getDelay(null)).intValue();
	}

	@Override
	public long getDelay(TimeUnit unit) {
		if (time < 0) {
			return 1;
		}
		return time - System.nanoTime();
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object retrieveData) {
		object = retrieveData;
	}
}
